const http = require("http");
const fs = require("fs");

http
  .createServer(function (request, response) {
    console.log("request come", request.url);
    const host = request.headers.host;
    if (request.url === "/") {
      const html = fs.readFileSync("test.html", "utf-8");
      if (host === "test.com") {    // 浏览器访问的所有test.com域名下的接口都会返回这个cookie，包括test.com下的所有二级域名
        response.writeHead(200, {
          "Content-Type": "text/html",
          "Set-Cookie": ["id=123; max-age=2", "abc=456; HttpOnly; domain=test.com"], // 服务器给浏览器设置cookie，设置HttpOnly可以禁止前端访问该条cookie，设置domain=test.com表示test.com下的所有二级域名都可以访问这个cookie
        });
      }
      response.end(html);
    }
  })
  .listen(8888);
console.log("server listening on 8888");
